package com.app.view.component.hall 
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	
	import org.component.List;
	import org.component.events.ListEvent;
	import org.component.interfaces.IListItemData;
	
	import com.peachcastle.core.Display;
	
	import com.peachcastle.events.HallEvent;
	import com.app.model.data.HallListItemData;
	
	import com.app.view.component.HallMediator;
	import com.app.view.component.hall.item.HallListItem;
	
	import com.peachcastle.tools.Assert;
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	[Event(name = "hall_select_item", type = "com.peachcastle.events.HallEvent")]
	public class HallList extends Display
	{

		private var _list:List = null;
		
		private var _animationView:MovieClip = null;	//房间列表loading条
		
		public function HallList(ui:MovieClip = null) 
		{
			super(ui);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			
			//断言
			Assert.hasNot(assest, 'animationView', 'HallList');
			
			_animationView = assest["animationView"];
			_animationView.stop();
			_animationView.visible = false;

			_list = new List(assest, HallListItem, 1, 11, 30);
			_list.update([]);
		}
		
		override protected function configEventListeners():void 
		{
			super.configEventListeners();
			
			_list.addEventListener(ListEvent.LIST_SELECT, onSelectList);
			_list.addEventListener(ListEvent.LIST_SELECT_ROOM, onSelectRoom);
		}
		
		private function onSelectList(event:ListEvent):void
		{
			var data:HallListItemData = event.item.listdata as HallListItemData;
			var evt:HallEvent = new HallEvent(HallEvent.HALL_SELECT_ITEM);
			evt.data = data;
			dispatchEvent(evt);
			evt = null;
		}
		
		private function onSelectRoom(event:ListEvent):void
		{
			var data:HallListItemData = event.item.listdata as HallListItemData;
			var evt:HallEvent = new HallEvent(HallEvent.HALL_SELECT_ROOM);
			evt.data = data;
			dispatchEvent(evt);
			evt = null;
		}
		
		private var _roomList:Array = null;
		
		public function serverUpdate(data:Array = null):void
		{
			_roomList = data;
			_list.update(_roomList);
		}
		
		public function serverUpdateRoom(newHl:IListItemData):void
		{
			Log.debug("HallList update room", "roomid=" + HallListItemData(newHl).roomid, "count=" + HallListItemData(newHl).currMember);
			
			var hl:HallListItem = _list.getTypeByItem("roomid", HallListItemData(newHl).roomid) as HallListItem;
			if (hl)
			{
				hl.update(newHl);
			}
		}
	}

}